package org.gdpu.service.impl;

import org.gdpu.base.DaoSupport;
import org.gdpu.base.PageView;
import org.gdpu.model.BloodOrder;
import org.gdpu.service.BloodOrderService;
import org.gdpu.vo.BloodOrderVO;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.persistence.Query;
import java.util.*;

/**
 * Created with IntelliJ IDEA.
 * User: Administrator
 * Date: 13-11-7
 * Time: 上午9:15
 * To change this template use File | Settings | File Templates.
 */
@Service
@Transactional
public class BloodOrderServiceImpl extends DaoSupport<BloodOrder> implements BloodOrderService {

    @Override
    public List<BloodOrder> allBloodOrder() {
        Query query = em.createQuery("from BloodOrder bo");
        return query.getResultList();
    }

    @Override
    public List<BloodOrder> allBloodOrderByDate(Date begin,Date end){
        Query query = em.createQuery("select bo from BloodOrder bo where bo.applyTime > ?1 and bo.applyTime < ?2 ");
        query.setParameter(1, begin).setParameter(2, end);
        return query.getResultList();
    }

    @Override
    public PageView<BloodOrder> findBloodOrder(BloodOrderVO bloodOrderVO) {
        /** 每页显示的结果数 **/
        int maxResult = 10;
        /** 封装的页面数据 **/
        PageView<BloodOrder> pageView = new PageView<BloodOrder>(maxResult, bloodOrderVO.getPageNow());
        int firstIndex = ((pageView.getCurrentPage() - 1) * pageView.getMaxResult());// 开始索引
        // 按照条件排序
        LinkedHashMap<String, String> orderBy = new LinkedHashMap<String, String>();
        orderBy.put("state", "asc");
        /** 列表条件 **/
        StringBuffer jpql = new StringBuffer("o.state = ?");
        /** 列表条件的值 **/
        List<Object> params = new ArrayList<Object>();
        params.add(0);
        pageView.setQueryResult(this.getScrollData(BloodOrder.class, firstIndex, maxResult, jpql.toString(),
                params.toArray(), orderBy));
        return pageView;
    }

    @Override
    public void provide(int[] applyBloodIds, String[] messages) {
        Query query;

        for (int i = 0; i < applyBloodIds.length; i++) {
            if ("已发血".equals(messages[i]))
                review(applyBloodIds[i], 1);
            else
                review(applyBloodIds[i], 2);
            query = em.createQuery("update BloodOrder bloodOrder set bloodOrder.checkPerson = '陈医生' ,bloodOrder.message = '" + messages[i] +
                    "' where bloodOrder.applyBloodId = " + applyBloodIds[i]);
            query.executeUpdate();
        }
    }

    private void review(int applyBloodId, int state) {
        Query query = em.createQuery("update BloodOrder bloodOrder set bloodOrder.state = ?1 where bloodOrder.applyBloodId = ?2");
        query.setParameter(1, state);
        query.setParameter(2, applyBloodId);
        query.executeUpdate();
    }

    public List<BloodOrder> findBloodByOffice(String officeName) {
        Query query = em.createQuery("select bo from BloodOrder bo where bo.officeName.officeId=" + officeName + " and bo.state=1");
        return query.getResultList();
    }
}
